//
//  USAlbumCoverColorChooseBar.m
//  us
//
//  Created by karry on 2025/4/11.
//

#import "USAlbumCoverColorChooseBar.h"
#import "USAlbumCell.h"

@interface USAlbumCoverColorChooseBar()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) NSArray <NSString *>*macaronColors;

@end

@implementation USAlbumCoverColorChooseBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    
//    NSArray<NSString *> *macaronColors = @[
//        // 🔴 Red
//        @"#F4D8D6", @"#E59F98", @"#D6655B", @"#B7392D",
//        // 🟠 Orange
//        @"#F4E2D6", @"#E5B898", @"#D68E5B", @"#B7672D",
//        // 🟡 Yellow
//        @"#F4EFD6", @"#E5D898", @"#D6C15B", @"#B7A02D",
//        // 🟢 Green
//        @"#DBF4D6", @"#A5E598", @"#70D65B", @"#44B72D",
//        // 🔵 Blue
//        @"#D6E5F4", @"#98BFE5", @"#5B98D6", @"#2D72B7",
//        // 🟦 Cyan
//        @"#D6F4F2", @"#98E5DF", @"#5BD6CC", @"#2DB7AC",
//        // 🟣 Purple
//        @"#E8D6F4", @"#C598E5", @"#A35BD6", @"#7E2DB7",
//        // ⚫️ Gray to Black
//        @"#BFBFBF", @"#999999", @"#727272", @"#4C4C4C"
//    ];
//
//    
//    NSArray<NSString *> *macaronColors = @[
//        // 🌸 粉红
//        @"#F4D6E0", @"#E598B2", @"#D65B84", @"#B72D5B",
//        // 🔴 红
//        @"#F4D8D6", @"#E59F98", @"#D6655B", @"#B7392D",
//        // 🟠 橙
//        @"#F4E2D6", @"#E5B898", @"#D68E5B", @"#B7672D",
//        // 🟡 黄
//        @"#F4EFD6", @"#E5D898", @"#D6C15B", @"#B7A02D",
//        // 🟢 绿
//        @"#DBF4D6", @"#A5E598", @"#70D65B", @"#44B72D",
//        // 🔵 蓝
//        @"#D6E5F4", @"#98BFE5", @"#5B98D6", @"#2D72B7",
//        // 🟦 青
//        @"#D6F4F2", @"#98E5DF", @"#5BD6CC", @"#2DB7AC",
//        // 🟣 紫
//        @"#E8D6F4", @"#C598E5", @"#A35BD6", @"#7E2DB7",
//        // ⚫️ 灰黑
//        @"#BFBFBF", @"#999999", @"#727272", @"#4C4C4C"
//    ];
    
    
    NSArray<NSString *> *vividMacaronColors = @[
        // 🌸 粉红
        @"#F8DCE5", @"#F1B0C6", @"#E985A6", @"#E15A87",
        // 🔴 红
        @"#F8DCDC", @"#F1B0B0", @"#E98585", @"#E15A5A",
        // 🟠 橙
        @"#F8E8DC", @"#F1CBB0", @"#E9AF85", @"#E1925A",
        // 🟡 黄
        @"#F8F4DC", @"#F1E6B0", @"#E9D885", @"#E1CB5A",
        // 🟢 绿
        @"#E1F8DC", @"#BBF1B0", @"#96E985", @"#70E15A",
        // 🔵 蓝
        @"#DCEAF8", @"#B0D1F1", @"#85B7E9", @"#5A9EE1",
        // 🟦 青
        @"#DCF8F4", @"#B0F1E6", @"#85E9D8", @"#5AE1CB",
        // 🟣 紫
        @"#EADCF8", @"#D1B0F1", @"#B785E9", @"#9E5AE1",
        // ⚫️ 灰黑
        @"#E5E5E5", @"#BFBFBF", @"#999999", @"#666666",@"#212121"
    ];
    
    NSArray<NSString *> *morandiColors = @[
        // 🌸 粉红
        @"#D7C0C8", @"#C3A1AC", @"#B9929F", @"#A57383",
        // 🔴 红
        @"#D7C0C0", @"#C3A1A1", @"#B99292", @"#A57373",
        // 🟠 橙
        @"#D7CAC0", @"#C3AFA1", @"#B9A292", @"#A58873",
        // 🟡 黄
        @"#D7D3C0", @"#C3BDA1", @"#B9B292", @"#A59D73",
        // 🟢 绿
        @"#C4D7C0", @"#A7C3A1", @"#98B992", @"#7BA573",
        // 🔵 蓝
        @"#C0CCD7", @"#A1B2C3", @"#92A5B9", @"#738CA5",
        // 🟦 青
        @"#C0D7D3", @"#A1C3BD", @"#92B9B2", @"#73A59D",
        // 🟣 紫
        @"#CCC0D7", @"#B2A1C3", @"#A592B9", @"#8C73A5",
        //⚫️ 灰黑
    ];
    NSMutableArray *all = [NSMutableArray array];
    [all addObjectsFromArray:vividMacaronColors];
    [all addObjectsFromArray:morandiColors];
    self.macaronColors = all;
    
    self.backgroundColor = [UIColor mi_1_color];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(40, 40 * 424 /345.f);
    layout.minimumLineSpacing = 12;
    layout.sectionInset = UIEdgeInsetsMake(16, 0, 16, 12);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = UIColor.clearColor;
    [collectionView registerClass:[USAlbumCell class] forCellWithReuseIdentifier:@"USAlbumCellId"];
    _collectionView = collectionView;
    [self addSubview:collectionView];

    UIButton *addBgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBgBtn setBackgroundImage:[UIImage jk_imageWithColor:[UIColor mi_3_color]] forState:0];
    [addBgBtn setImage:[UIImage imageNamed:@"chat_tool_pic_1"] forState:0];
    addBgBtn.layer.cornerRadius = 6;
    addBgBtn.layer.masksToBounds = YES;
    [self addSubview:addBgBtn];
    _addBgBtn = addBgBtn;
    
    [addBgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(12);
        make.width.mas_equalTo(layout.itemSize.width);
        make.height.mas_equalTo(layout.itemSize.height);
        make.centerY.mas_equalTo(self);
    }];

    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_offset(0);
        make.left.mas_equalTo(addBgBtn.mas_right).mas_offset(12);
    }];

    [self.heightAnchor constraintEqualToConstant:layout.itemSize.height + 16 *2].active = YES;
    
}

- (void)setColor:(UIColor *)color{
    _color = color;
    
    NSIndexPath *indexPath = nil;
    for (NSInteger i = 0; i < self.macaronColors.count; i++) {
        
        UIColor *c_color = [UIColor jk_colorWithHexString:self.macaronColors[i]];
        
        if (CGColorEqualToColor(color.CGColor, c_color.CGColor)) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }
    
    if (indexPath) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        });
    }
}

#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.macaronColors.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [USFeedbackTool selectionChanged];
    [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    !self.didChooseColorBlock?:self.didChooseColorBlock([UIColor jk_colorWithHexString:[self.macaronColors objectAtIndex:indexPath.row]]);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    USAlbumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"USAlbumCellId" forIndexPath:indexPath];
    
    cell.coverView.color = [UIColor jk_colorWithHexString:self.macaronColors[indexPath.row]];
    
    cell.coverView.showBookmark = NO;
    
    cell.seletedActive = YES;
    
    return cell;
    
}

@end
